package uit.ce03.game_ninja.quaivat;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXLayer;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXProperties;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXTile;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXTileProperty;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXTiledMap;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;




import android.content.Context;

public class quaivat {
	public int max_quai = 10; 
	public static int speed =2;
	
	public Status_QuaiVat[] quaivat_1;
	

	private TiledTextureRegion[] Quaivat_TiledTextureRegion;
	private BitmapTextureAtlas Quaivat_BitmapTextureAtlas;	
	
	private TMXTiledMap mTMXTiledMap;
	private TMXLayer VatCanTMXLayer;
	
	
	private Scene mScene;
	
	//==================================================================================
	
	
	public quaivat(int max_quai){
		this.max_quai = max_quai;
		quaivat_1 = new Status_QuaiVat[this.max_quai];
		Quaivat_TiledTextureRegion = new TiledTextureRegion[this.max_quai];
	}
	//==================================================================================
	
	public void onLoadResources(Engine mEngine, Context mContext) {
		this.Quaivat_BitmapTextureAtlas = new BitmapTextureAtlas(512, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("quaivat/");
		for(int i=0;i<this.max_quai;i++)
			this.Quaivat_TiledTextureRegion[i] = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.Quaivat_BitmapTextureAtlas,
					mContext, "quaivat.png", 0, 0, 12, 4);
		mEngine.getTextureManager().loadTexture(this.Quaivat_BitmapTextureAtlas);			
			
	}
	//==================================================================================
	
	
	public void onLoadScene(Scene mScene) {
		this.mScene = mScene;
		for(int i=0;i<this.max_quai;i++){	
			quaivat_1[i] = new Status_QuaiVat(this, mScene,-100,-100,this.Quaivat_TiledTextureRegion[i]);
		}
			
	}
	public void setTMXTiledMap(TMXTiledMap mTMXTiledMap){
		this.mTMXTiledMap = mTMXTiledMap;
	}
	public void setTMXLayer(TMXLayer VatCanTMXLayer){
		this.VatCanTMXLayer = VatCanTMXLayer;
	}
	public boolean checkQuaiVat(TMXTile mTMXTile){
		try{
			if(mTMXTile != null){
				TMXProperties<TMXTileProperty> mTMXProperties= mTMXTile.getTMXTileProperties(mTMXTiledMap);
				TMXTileProperty mTMXTileProperty = mTMXProperties.get(0);
				if(mTMXTileProperty.getName().equals("vatcan_quaivat")){	
					return true;
				}else{
					
					return false;
				}
			}
		}catch(Exception e){
			
			return false;
		}
		return false;
	}
	
	public void reset(){
		for(int i = 0; i<max_quai;i++)
		{
			boolean check = true;
			boolean run = false, run1 = false;
			
			while(check == true)
			{
				int x = getRandomIndex(20, 690);
				int y = getRandomIndex(0,166);
				TMXTile mTMXTile = VatCanTMXLayer.getTMXTileAt(x+ 32, y+ 32);
				run = checkQuaiVat(mTMXTile);
				TMXTile mTMXTile1 = VatCanTMXLayer.getTMXTileAt(x+16, y+16);
				run1 = checkQuaiVat(mTMXTile1);
				if(run && run1)
				{
					quaivat_1[i] = new Status_QuaiVat(this, this.mScene,x,y,this.Quaivat_TiledTextureRegion[i]);
					check = false;
				}
			}
			
		}
		
	}
	boolean run = false, run1 = false;
	public float toadoleftX(AnimatedSprite mAnimatedSprite)
	{
		float x = mAnimatedSprite.getX() - speed;
		return x;
	}
	public float toadoleftY(AnimatedSprite mAnimatedSprite)
	{
		float y = mAnimatedSprite.getY() + mAnimatedSprite.getHeight();
		return y;
	}
	public float toadorightX(AnimatedSprite mAnimatedSprite)
	{
		 float x = mAnimatedSprite.getX() + mAnimatedSprite.getWidth() + speed;
		return x;
	}
	public float toadorightY(AnimatedSprite mAnimatedSprite)
	{
		float y = mAnimatedSprite.getY() + mAnimatedSprite.getHeight();
		return y;
	}
	
	public boolean checkLeft(float x, float y)
	{
		TMXTile mTMXTile = VatCanTMXLayer.getTMXTileAt(x, y);
		run = checkQuaiVat(mTMXTile);
		if(run)
			return true;
		else 
			return false;
		
	}

	//==================================================================================
	
	
	//==================================================================================
	
	public static int getRandomIndex(int min, int max) {
		return (int) (Math.random() * (max - min + 1)) + min;
	}
	public void reset(Status_QuaiVat quaivat){
		quaivat.mSta_AnimatedSprite.setVisible(true);
		quaivat.mSta_AnimatedSprite.setPosition(416,128);
	}
}